package org.example.tmsapi.service.impl;

import org.example.tmsapi.model.Order;
import org.example.tmsapi.model.OrderLog;
import org.example.tmsapi.service.OrderService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {
    
    // 模拟数据库存储订单数据
    private static final Map<String, Order> orderMap = new HashMap<>();
    // 模拟数据库存储订单日志数据
    private static final Map<String, List<OrderLog>> orderLogMap = new HashMap<>();
    
    // 静态初始化，添加一些模拟数据
    static {
        Order order = new Order();
        order.setOrderId("665146");
        order.setCreateTime(new Date(System.currentTimeMillis() - 86400000)); // 昨天
        order.setOrderStatus("配送中");
        order.setOrderNo("ST717003101442540");
        order.setTrackingNo("ST717003101442540991");
        order.setCustomerName("张三");
        order.setContactNumber("13800138000");
        order.setSpecialRemarks("优先配送");
        order.setOrderType("普通订单");
        order.setPlatform("平台A");
        order.setPlatformOrderNo("ST717003101442540");
        order.setQuantity(1);
        order.setProductName("测试商品");
        order.setWeight(15.5);
        order.setUnit("kg");
        order.setSpecialProduct("否");
        order.setLogisticsNoteNo("KD00000000000001");
        order.setLogisticsCompany("快递公司A");
        
        orderMap.put(order.getOrderId(), order);
        
        // 添加模拟日志
        List<OrderLog> logs = new ArrayList<>();
        OrderLog log1 = new OrderLog();
        log1.setOperationTime(new Date(System.currentTimeMillis() - 3600000)); // 1小时前
        log1.setOperator("系统管理员");
        log1.setOperationContent("订单创建");
        log1.setOrderId(order.getOrderId());
        
        OrderLog log2 = new OrderLog();
        log2.setOperationTime(new Date(System.currentTimeMillis() - 1800000)); // 30分钟前
        log2.setOperator("张三");
        log2.setOperationContent("订单已分配");
        log2.setOrderId(order.getOrderId());
        
        logs.add(log1);
        logs.add(log2);
        orderLogMap.put(order.getOrderId(), logs);
    }
    
    @Override
    public Order getOrderById(String orderId) {
        return orderMap.get(orderId);
    }
    
    @Override
    public List<Order> searchOrders(String orderNo, String trackingNo, String customerName) {
        return orderMap.values().stream()
                .filter(order -> (orderNo == null || orderNo.isEmpty() || order.getOrderNo().contains(orderNo))
                        && (trackingNo == null || trackingNo.isEmpty() || order.getTrackingNo().contains(trackingNo))
                        && (customerName == null || customerName.isEmpty() || order.getCustomerName().contains(customerName)))
                .collect(Collectors.toList());
    }
    
    @Override
    public List<OrderLog> getOrderLogs(String orderId) {
        List<OrderLog> logs = orderLogMap.get(orderId);
        return logs != null ? logs : new ArrayList<>();
    }
    
    @Override
    public void addOrderLog(String orderId, String operator, String operationContent) {
        OrderLog log = new OrderLog();
        log.setOperationTime(new Date());
        log.setOperator(operator);
        log.setOperationContent(operationContent);
        log.setOrderId(orderId);
        
        orderLogMap.computeIfAbsent(orderId, k -> new ArrayList<>()).add(log);
    }
    
    @Override
    public boolean updateOrderStatus(String orderId, String newStatus, String operator) {
        Order order = orderMap.get(orderId);
        if (order == null) {
            return false;
        }
        
        String oldStatus = order.getOrderStatus();
        order.setOrderStatus(newStatus);
        
        // 添加状态变更日志
        addOrderLog(orderId, operator, "订单状态从\"" + oldStatus + "\"变更为\"" + newStatus + "\"");
        
        return true;
    }
}